﻿using EYWeb.Core;
using EYWeb.Core.Cache;
using EYWeb.Domain.Entities;
using EYWeb.Repositories;
using EYWeb.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EYWeb.Services
{
    public class RoleAuthorizeService : BaseService<RoleAuthorizeEntity>, IRoleAuthorizeService
    {
        private readonly IRoleAuthorizeRepository _roleAuthorizeRepository;
        private readonly IModuleRepository _moduleRepository;
        private readonly IModuleButtonRepository _moduleButtonRepository;
        public RoleAuthorizeService(IRoleAuthorizeRepository roleAuthorizeRepository, IModuleRepository moduleRepository, IModuleButtonRepository moduleButtonRepository) : base(roleAuthorizeRepository)
        {
            _roleAuthorizeRepository = roleAuthorizeRepository;
            _moduleRepository = moduleRepository;
            _moduleButtonRepository = moduleButtonRepository;
        }

        public bool ActionValidate(string roleId, string moduleId, string action)
        {

            List<AuthorizeActionModel> authorizeList = new List<AuthorizeActionModel>();
            //从缓存中获取权限数据
            var cacheDate = CacheFactory.Cache.GetCache<List<AuthorizeActionModel>>("authorizeData" + roleId);
            if (cacheDate == null)
            {
                var moduleData = _moduleRepository.FindAll().ToList();
                var moduleButtonData = _moduleButtonRepository.FindAll().ToList();
                var authorizeData = _roleAuthorizeRepository.FindListByClause(t => t.ObjectId == roleId);
                foreach (var item in authorizeData)
                {
                    if(item.ItemType == (int)ProjectType.Module)
                    {
                        var moduleEntity = moduleData.Find(t=>t.Id == item.ItemId);
                        if(moduleEntity != null)
                        {
                            authorizeList.Add(new AuthorizeActionModel { Id = moduleEntity.Id, UrlAddress = moduleEntity.UrlAddress });
                        }
                        
                    }
                    if(item.ItemType == (int)ProjectType.Button)
                    {
                        var moduleButtonEntity = moduleButtonData.Find(t => t.Id == item.ItemId);
                        if(moduleButtonEntity!= null)
                        {
                            authorizeList.Add(new AuthorizeActionModel() { Id = moduleButtonEntity.ModuleId, UrlAddress = moduleButtonEntity.UrlAddress });
                        }
                        
                    }
                }
                CacheFactory.Cache.WriteCache<List<AuthorizeActionModel>>("authorizeData" + roleId, authorizeList, DateTime.Now.AddMinutes(5));

            }
            else
            {
                authorizeList = cacheDate;
            }
            authorizeList = authorizeList.FindAll(t => t.Id.Contains(moduleId));
            foreach (var item in authorizeList)
            {
                if(!item.UrlAddress.IsEmpty())
                {
                    string[] url = item.UrlAddress.Split('?');
                    if(item.Id==moduleId && url[0] == action)
                    {
                        return true;
                    }
                }
            }
            return false;

        }

        
        

        



    }
}
